package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;


public class Email {

	public static int lastEmlIdDb=0;
	private int eml_id=0;
	private String email = "";

	//costruttore email per nuovo inserimento
	public Email() {
		//set_LastEmlIDfromDB(get_LastEmlIDfromDB());
		/*this.eml_id = Email.lastEmlIdDb;
		Email.lastEmlIdDb++;*/
	}

	//costruttore email con dati
	public Email(int eml_id, String email){
		this.setEml_id(eml_id);
		this.setEmail(email);
		/*if (this.eml_id>Email.lastEmlIdDb)
			Email.lastEmlIdDb = this.eml_id;*/
	}

	/*public int get_LastEmlIdDb() {
		return lastEmlIdDb;
	}*/

	public void set_LastEmlIDfromDB() {
		Email.lastEmlIdDb++;
	}

	//estrazione risorse dal db per
	public int get_LastEmlIDfromDB() {
		ResultSet result = Database.getQueryResult("SELECT eml_id FROM email");
		int id=0;
		try {
			while(result.next())
				id=result.getInt("eml_id");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Email.lastEmlIdDb=id+1;
		return id+1;
	}
	public int getEml_id() {
		return eml_id;
	}

	public void setEml_id(int eml_id) {
		this.eml_id = eml_id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public static Vector<Email> accEml(int acc_id){
		Vector<Email> eml = new Vector<Email>();
		try {
			ResultSet resultEml = Database.getQueryResult("SELECT * FROM email WHERE acc_id="+acc_id);
			while (resultEml.next())
				eml.add(new Email(resultEml.getInt("eml_id"),resultEml.getString("eml")));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return eml;
	}
}